package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class HJ17_机房布局 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/17.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    static void Solution(Scanner in) {
        char[] cArray = in.nextLine().toCharArray();
        int index = 0;
        int res = 0;
        while (index < cArray.length) {
            char now = cArray[index];
            if (now == 'M') {
                if (index + 1 < cArray.length) {
                    char next = cArray[index + 1];
                    if (next == 'I') {
                        res++;
                        index += 3;
                        continue;
                    }
                }
                if (index > 0) {
                    char before = cArray[index - 1];
                    if (before == 'I') {
                        res++;
                        index++;
                        continue;
                    }
                }
                System.out.println(-1);
                return;
            } else if (now == 'I') {
                index++;
            }
        }
        System.out.println(res);
    }
}